<?php
class PayDAO extends DAO {

	public static function getorder($orderid = 0)
	{
		$orderid = intval($orderid);
		if(!$orderid) return false;

		$dbconf = self::dbhash(0, "pay", "pay");
        $dbconn = self::getconn(0, $dbconf, __FUNCTION__);
        if (!$dbconn) return false;
        
        $sql = "select orderid, uid, amount, mb, tm, stat, token from {$dbconf[4]} where orderid={$orderid}";
        $row = self::dbquery($dbconn, $sql, "row");
		self::dbclose($dbconn);

		return $row;
	}

	public static function getorderbytoken($token = 0)
	{
		$token = mysql_escape_string($token);
		if(!$token) return false;

		$dbconf = self::dbhash(0, "pay", "pay");
        $dbconn = self::getconn(0, $dbconf, __FUNCTION__);
        if (!$dbconn) return false;
        
        $sql = "select orderid, uid, amount, mb, tm, stat, token from {$dbconf[4]} where token={$token}";
        $row = self::dbquery($dbconn, $sql, "row");
		self::dbclose($dbconn);

		return $row;
	}
    
    public static function getpay($uid = 0)
	{
        $uid = intval($uid);
        if (!$uid) return false;
        
        $ret = array();
        $dbconf = self::dbhash(0, "pay", "pay");
        $dbconn = self::getconn(0, $dbconf, __FUNCTION__);
        if (!$dbconn) return false;
        
        $sql = "select orderid, uid, amount, mb, tm, stat from {$dbconf[4]} where uid={$uid}";
        $res = self::dbquery($dbconn, $sql);
        if ($res && mysql_num_rows($res) >= 1)
		{
            while ($row = mysql_fetch_assoc($res))
			{
				$ret[$row["orderid"]] = $row;
            }
            unset($row);
            if (!$ret) $ret = array();
        }
		elseif ($res) $ret = array();
        else $ret = false;
        
        self::dbclose($dbconn);

        return $ret;
    }

    public static function newpay($uid = 0, $amount = 0, $mb = 0)
	{
        $uid = intval($uid);
		$amount = intval($amount);
		$mb = intval($mb);
        if (!$uid || !$amount || !$mb) return false;
        
        $ret = false;
        $dbconf = self::dbhash(0, "pay", "pay");
        $dbconn = self::getconn(0, $dbconf, __FUNCTION__);
        if (!$dbconn) return false;

		$tm = time();
        
		$sql = "insert into {$dbconf[4]} (uid, amount, mb, tm) values({$uid}, {$amount}, {$mb}, {$tm})";
		if (self::dbquery($dbconn, $sql)) $ret = self::dbid($dbconn);
        self::dbclose($dbconn);

        return $ret;
    }

	public static function newpayall($uid = 0, $row = array())
	{
        $uid = intval($uid);

		if(!$uid || !$row) return false;
		
		$set = array();
		foreach($row as $k => $v)
		{
			switch($k)
			{
				case "amount":
				case "mb":
				case "stat":
				case "tm":
					$set[$k] = "{$k}=" . intval($v);
					break;
				case "token":
					$set[$k] = "{$k}='" . mysql_escape_string($v) . "'";
					break;
			}
		}
		$set["uid"] = "uid=" . $uid;
		$set["tm"] = $set["tm"] ? $set["tm"] : "tm=" . time();
		$set = implode(",", $set);
        
        $ret = false;
        $dbconf = self::dbhash(0, "pay", "pay");
        $dbconn = self::getconn(0, $dbconf, __FUNCTION__);
        if (!$dbconn) return false;
        
		$sql = "insert into {$dbconf[4]} set {$set}";
		if (self::dbquery($dbconn, $sql)) $ret = self::dbid($dbconn);
        self::dbclose($dbconn);

        return $ret;
    }

	/*
	** row = array("amount" => $amount, "mb" => $mb, "stat" => $stat, "token" => $token)
	*/
	public static function setpay($orderid = 0, &$row = array())
	{
		$orderid = intval($orderid);
            
        if (!$orderid || !is_array($row) || !$row) return false;

		$sets = array();
		if(isset($row["amount"]))		$sets[] = "amount='" . intval($row["amount"]) . "'";
		if(isset($row["mb"]))			$sets[] = "mb='" . intval($row["mb"]) . "'";
		if(isset($row["stat"]))			$sets[] = "stat='" . intval($row["stat"]) . "'";
		if(isset($row["token"]))		$sets[] = "token='" . mysql_escape_string($row["token"]) . "'";
		if (!$sets) return false;

		$sets = implode(",", $sets);
		if (!$sets) return false;

		$dbconf = self::dbhash(0, "pay", "pay");
        $dbconn = self::getconn(0, $dbconf, __FUNCTION__);
        if (!$dbconn) return false;

		$sql = "update {$dbconf[4]} set {$sets} where orderid={$orderid}";
                

		$ret = false;
		if (self::dbquery($dbconn, $sql)) $ret = true;
        self::dbclose($dbconn);
        return $ret;
	}

    public static function delpay($orderid = 0)
	{
		$orderid = intval($orderid);
        if (!$orderid) return false;
        
        $ret = false;
        $dbconf = self::dbhash(0, "pay", "pay");
        $dbconn = self::getconn(0, $dbconf, __FUNCTION__);
        if (!$dbconn) return false;
        
		$sql = "DELETE FROM {$dbconf[4]} WHERE orderid={$orderid}";
		if (self::dbquery($dbconn, $sql)) $ret = true;
        self::dbclose($dbconn);
        return $ret;
    }

	public static function logpay($orderid = 0, $content = "")
	{
	}
        
}
